# 洗护系统API测试脚本
$baseUrl = "http://localhost:8080"

Write-Host "=== 洗护系统API测试 ===" -ForegroundColor Green

# 1. 测试用户注册
Write-Host "`n1. 测试用户注册..." -ForegroundColor Yellow
$registerData = @{
    username = "testuser"
    email = "test@example.com"
    password = "123456"
} | ConvertTo-Json

try {
    $registerResponse = Invoke-RestMethod -Uri "$baseUrl/api/auth/register" -Method POST -ContentType "application/json" -Body $registerData
    Write-Host "注册成功: $($registerResponse | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "注册失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 2. 测试用户登录
Write-Host "`n2. 测试用户登录..." -ForegroundColor Yellow
$loginData = @{
    username = "testuser"
    password = "123456"
} | ConvertTo-Json

try {
    $loginResponse = Invoke-RestMethod -Uri "$baseUrl/api/auth/login" -Method POST -ContentType "application/json" -Body $loginData
    $token = $loginResponse.token
    Write-Host "登录成功，获取到Token: $token" -ForegroundColor Green
    
    # 设置认证头
    $headers = @{
        "Authorization" = "Bearer $token"
        "Content-Type" = "application/json"
    }
} catch {
    Write-Host "登录失败: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

# 3. 测试获取当前用户信息
Write-Host "`n3. 测试获取当前用户信息..." -ForegroundColor Yellow
try {
    $userInfo = Invoke-RestMethod -Uri "$baseUrl/api/auth/me" -Method GET -Headers $headers
    Write-Host "用户信息: $($userInfo | ConvertTo-Json)" -ForegroundColor Green
} catch {
    Write-Host "获取用户信息失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 4. 测试发送验证码
Write-Host "`n4. 测试发送验证码..." -ForegroundColor Yellow
$verificationData = @{
    email = "test@example.com"
    type = "REGISTER"
} | ConvertTo-Json

try {
    $verificationResponse = Invoke-RestMethod -Uri "$baseUrl/api/auth/send-verification-code" -Method POST -ContentType "application/json" -Body $verificationData
    Write-Host "验证码发送成功: $verificationResponse" -ForegroundColor Green
} catch {
    Write-Host "发送验证码失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 5. 测试获取所有订单
Write-Host "`n5. 测试获取所有订单..." -ForegroundColor Yellow
try {
    $allOrders = Invoke-RestMethod -Uri "$baseUrl/api/laundry/orders" -Method GET -Headers $headers
    Write-Host "获取订单成功，共 $($allOrders.totalElements) 个订单" -ForegroundColor Green
} catch {
    Write-Host "获取订单失败: $($_.Exception.Message)" -ForegroundColor Red
}

# 6. 测试H2控制台访问
Write-Host "`n6. 测试H2控制台访问..." -ForegroundColor Yellow
try {
    $h2Console = Invoke-WebRequest -Uri "$baseUrl/h2-console" -Method GET
    Write-Host "H2控制台可访问，状态码: $($h2Console.StatusCode)" -ForegroundColor Green
} catch {
    Write-Host "H2控制台访问失败: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n=== API测试完成 ===" -ForegroundColor Green
Write-Host "请检查上述测试结果，确保所有功能正常工作。" -ForegroundColor Cyan 